using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace PhotoFieldControl
{
    class PhotoField : SPFieldMultiColumn
    {
        #region Contstructors

        public PhotoField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {

            this.Init();

        }

        public PhotoField(Microsoft.SharePoint.SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {

            this.Init();
        }


        #endregion
        
        #region properties

        public int ContextId
        {

            get
            {

                return SPContext.Current.GetHashCode();

            }

        }

        

        private static Dictionary<int, FieldProps> updatedApplicationProperty = new Dictionary<int, FieldProps>(); 

        private void Init()
        {
            object o = this.GetCustomProperty("UseThisSite");
            this.useThisWeb = Convert.ToBoolean(o);
            this.webUrl = this.GetCustomProperty("WebSiteUrl") + "";
            this.listName = this.GetCustomProperty("ListName") + "";

        }

        public void UpdateApplicationProperty(FieldProps value)
        {
            updatedApplicationProperty[ContextId] = value;
        } 

        

        private bool useThisWeb;

        public bool UseThisWeb
        {
            get {
            
                

                if (updatedApplicationProperty.ContainsKey(ContextId))
                {

                    //If we saved a value away and we're in the middle of the save field process, then pull it out.
                    return updatedApplicationProperty[ContextId].UseThisWeb;
                }
                else
                {

                    //Return the existing value for the application property. This will be the case if we already have a field.
                    return useThisWeb;
                }


            
            }
            set { useThisWeb = value; }
        }

        private string webUrl;

        public string WebUrl
        {
            get {

                if (updatedApplicationProperty.ContainsKey(ContextId))
                {

                    //If we saved a value away and we're in the middle of the save field process, then pull it out.
                    return updatedApplicationProperty[ContextId].WebUrl;
                }
                else
                {

                    //Return the existing value for the application property. This will be the case if we already have a field.
                    return webUrl; 
                } 
            
            }
            set { webUrl = value; }
        }

        private string listName;

        public string ListName
        {
            get {
                if (updatedApplicationProperty.ContainsKey(ContextId))
                {

                    //If we saved a value away and we're in the middle of the save field process, then pull it out.
                    return updatedApplicationProperty[ContextId].ListName;
                }
                else
                {

                    //Return the existing value for the application property. This will be the case if we already have a field.
                    return listName;
                } 
            }
            set { listName = value; }
        }

        #endregion

        public override void Update()
        {

            this.SetCustomProperty("UseThisSite", this.UseThisWeb);
            this.SetCustomProperty("WebSiteUrl", this.WebUrl);
            this.SetCustomProperty("ListName", this.ListName);

            if (updatedApplicationProperty.ContainsKey(ContextId))
                updatedApplicationProperty.Remove(ContextId);


            base.Update();

            
        }

        public override void OnAdded(SPAddFieldOptions op)
        {

            base.OnAdded(op);

            Update();

        }





        /// <summary>
        /// This method validates the data as it is entered into the column. If it doesnt match the criteria a sharepoint exception is thrown.
        /// </summary>

        /// <param name="value"></param>

        /// <returns></returns>
        public override string GetValidatedString(object value)
        {
            return base.GetValidatedString(value);
        }


        public override object GetFieldValue(string value)
        {
            if (String.IsNullOrEmpty(value))
                return null;
            Microsoft.SharePoint.SPFieldMultiColumnValue photoValue = new Microsoft.SharePoint.SPFieldMultiColumnValue(value);
            return photoValue;
        

        }


        public override Microsoft.SharePoint.WebControls.BaseFieldControl FieldRenderingControl
        {

            get
            {

                Microsoft.SharePoint.WebControls.BaseFieldControl medewerkersFieldControl = new PhotoFieldControl();

                medewerkersFieldControl.FieldName = InternalName;

                return medewerkersFieldControl;

            }

        }


    }
}
